﻿IF EXISTS (SELECT * from sys.procedures p left join sys.schemas s on p.schema_id = s.schema_id where p.name='entity_select' and s.name='Crm')
DROP PROCEDURE [Crm].entity_select
GO
CREATE PROCEDURE [Crm].entity_select 
@PK uniqueidentifier
AS
BEGIN
SELECT 	
	[Entity].[id_Entity] AS [PK],
	[Entity].[manager] AS [Manager],
	[Entity].[name] AS [Name],
	[Entity].[company] AS [Company],
	[Entity].[title] AS [Title],
	[Entity].[phone] AS [Phone],
	[Entity].[mobile] AS [Mobile],
	[Entity].[source] AS [Source],
	[Entity].[url] AS [Url],
	[Entity].[mail] AS [Mail],
	[Entity].[fax] AS [Fax],
	[Entity].[status] AS [Status],
	[Entity].[skype] AS [Skype],
	[Entity].[twitter] AS [Twitter],
	[Entity].[sector] AS [Sector],
	[Entity].[no_mail] AS [NoMail],
	[Entity].[adress] AS [Adress],
	[Entity].[zip_code] AS [ZipCode],
	[Entity].[city] AS [City],
	[Entity].[country] AS [Country],
	[Entity].[description] AS [Description],
	[Meta].[References].[last_update] AS [LastUpdate],
	[Meta].[References].[Google] AS [Google],
	[Meta].[References].[Google_last_update] AS [GoogleLastUpdate],
	[Meta].[References].[ZohoCRM] AS [ZohoCRM],
	[Meta].[References].[ZohoCRM_last_update] AS [ZohoCRMLastUpdate],
	[Meta].[References].[from] AS [From],
	[Meta].[References].[Validated] AS [Validated]
FROM 
	[Entity] 
    INNER JOIN [Meta].[References] ON  [Entity].[id_Entity] = [References].[id_reference] 
WHERE
	[Entity].[id_Entity] = @PK 
END